﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.ContinuousMacro.CApCMGraphicsShapeDisplayer`1
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands.ContinuousMacro;

internal abstract class CApCMGraphicsShapeDisplayer<T> where T : class
{
  private T creationData;
  private bool isRecreatedOnAbsoluteLocationChanged;
  private Action DependencyObjectAbsoluteLocationChangedAction;

  protected bool IsRecreatedOnAbsoluteLocationChanged
  {
    get => this.isRecreatedOnAbsoluteLocationChanged;
    set
    {
      this.isRecreatedOnAbsoluteLocationChanged = value;
      if (this.isRecreatedOnAbsoluteLocationChanged)
        this.DependencyObjectAbsoluteLocationChangedAction = new Action(this.CreateDisplayableShape);
      else
        this.DependencyObjectAbsoluteLocationChangedAction = new Action(this.SetAbsoluteLocation);
    }
  }

  protected TxComponent DisplayParent { get; private set; }

  protected ITxLocatableObject DependencyObject { get; private set; }

  protected ITxDisplayableObject DisplayShape { get; private set; }

  public TxRoboticViaLocationOperation ViaLocation { get; private set; }

  public T CreationData
  {
    get => this.creationData;
    set
    {
      if ((object) this.creationData == (object) value)
        return;
      this.creationData = value;
      this.CreateDisplayableShape();
    }
  }

  public event Action<CApCMGraphicsShapeDisplayer<T>> Deleted;

  protected CApCMGraphicsShapeDisplayer(
    TxRoboticViaLocationOperation viaLocation,
    TxComponent displayParent,
    T creationData,
    ITxLocatableObject dependencyObject = null)
  {
    this.ViaLocation = viaLocation;
    this.DisplayParent = displayParent;
    this.DependencyObject = dependencyObject;
    this.CreationData = creationData;
    this.RegisterForEvents();
  }

  public void Delete()
  {
    if (this.Deleted != null)
      this.Deleted(this);
    this.UnRegisterFromEvents();
    this.DeleteDisplayableShape();
  }

  protected abstract ITxDisplayableObject CreateShape();

  private void RegisterForEvents()
  {
    this.ViaLocation.Deleted += new TxObject_DeletedEventHandler(this.DependencyObjectDeleted);
    this.ViaLocation.AbsoluteLocationChanged += new TxLocatableObject_AbsoluteLocationChangedEventHandler(this.DependencyObjectAbsoluteLocationChanged);
    this.ViaLocation.VisibilitySet += new TxDisplayableObject_VisibilitySetEventHandler(this.VisibilitySet);
    if (this.DependencyObject != null)
    {
      ((ITxObject) this.DependencyObject).Deleted += new TxObject_DeletedEventHandler(this.DependencyObjectDeleted);
      this.DependencyObject.AbsoluteLocationChanged += new TxLocatableObject_AbsoluteLocationChangedEventHandler(this.DependencyObjectAbsoluteLocationChanged);
    }
    ((TxBaseGeometryCreationObject) this.DisplayParent).Deleted += new TxObject_DeletedEventHandler(this.DependencyObjectDeleted);
  }

  private void UnRegisterFromEvents()
  {
    ((TxBaseGeometryCreationObject) this.DisplayParent).Deleted -= new TxObject_DeletedEventHandler(this.DependencyObjectDeleted);
    if (this.DependencyObject != null)
    {
      this.DependencyObject.AbsoluteLocationChanged -= new TxLocatableObject_AbsoluteLocationChangedEventHandler(this.DependencyObjectAbsoluteLocationChanged);
      ((ITxObject) this.DependencyObject).Deleted -= new TxObject_DeletedEventHandler(this.DependencyObjectDeleted);
    }
    this.ViaLocation.VisibilitySet -= new TxDisplayableObject_VisibilitySetEventHandler(this.VisibilitySet);
    this.ViaLocation.AbsoluteLocationChanged -= new TxLocatableObject_AbsoluteLocationChangedEventHandler(this.DependencyObjectAbsoluteLocationChanged);
    this.ViaLocation.Deleted -= new TxObject_DeletedEventHandler(this.DependencyObjectDeleted);
  }

  private void VisibilitySet(object sender, TxDisplayableObject_VisibilitySetEventArgs args)
  {
    if (this.DisplayShape == null)
      return;
    if (this.ViaLocation.Visibility == null)
      this.DisplayShape.Display();
    else
      this.DisplayShape.Blank();
  }

  private void DependencyObjectAbsoluteLocationChanged(
    object sender,
    TxLocatableObject_AbsoluteLocationChangedEventArgs args)
  {
    this.DependencyObjectAbsoluteLocationChangedAction();
  }

  private void DependencyObjectDeleted(object sender, TxObject_DeletedEventArgs args)
  {
    this.Delete();
  }

  private void CreateDisplayableShape()
  {
    this.DeleteDisplayableShape(false);
    if ((object) this.CreationData != null)
      this.DisplayShape = this.CreateShape();
    TxApplication.RefreshDisplay();
  }

  protected virtual void SetAbsoluteLocation()
  {
    if (this.DisplayShape == null)
      return;
    (this.DisplayShape as ITxLocatableObject).AbsoluteLocation = this.ViaLocation.AbsoluteLocation;
  }

  private void DeleteDisplayableShape(bool refreshDisplay = true)
  {
    if (this.DisplayShape == null)
      return;
    ((ITxObject) this.DisplayShape).Delete();
    this.DisplayShape = (ITxDisplayableObject) null;
    if (!refreshDisplay)
      return;
    TxApplication.RefreshDisplay();
  }
}
